package view.user;

import javax.naming.ldap.SortKey;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SortOrder;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableRowSorter;

import controller.ActionEvents.ActionHandler;



import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;

public class Leaderboard extends JPanel 
{
	
	/** Declares class variables */
	private JLabel lblLeaderboard; // Header
	private ActionHandler actionHandler;
	private JTable table;
	private DefaultTableModel model;
	private TableRowSorter sorter;
	private JScrollPane scrollPane;
	public String[] columnHeaders = {"ID", "Full Name", "Balance"};
	private Color tableColors = Color.WHITE;
	private Color tableShadeColors = Color.lightGray;
	
	/**
	 * Constructor creates the panel using CardLayout.
	 */
	public Leaderboard(ActionHandler actionHandler) 
	{
		setLayout(null);
		setPreferredSize(new Dimension(800, 372));
		this.actionHandler = actionHandler;
		setBackground(Color.WHITE);
		
		
		lblLeaderboard = new JLabel("Leaderboard");
		lblLeaderboard.setFont(new Font("Euphemia UCAS", Font.PLAIN, 28));
		lblLeaderboard.setBounds(321, 6, 163, 39);
		add(lblLeaderboard);
		
		model = new DefaultTableModel( actionHandler.getFrame().getModelClass().getLeaderboard(), columnHeaders ) 
		{
			Class[] columnTypes = new Class[] 
			{
					Integer.class, String.class, Double.class //kolonne datatyper
			};
			public Class getColumnClass(int columnIndex) 
			{
				return columnTypes[columnIndex];
			}
			boolean[] columnEditables = new boolean[] 
			{
				false, false, false
			};
			public boolean isCellEditable(int row, int column) 
			{
				return columnEditables[column];
			}
		};
		
		table = new JTable(){
			public Component prepareRenderer(TableCellRenderer renderer,int Index_row, int Index_col) {
				Component comp = super.prepareRenderer(renderer, Index_row, Index_col);
				//even index, selected or not selected
				if (Index_row % 2 == 0 && !isCellSelected(Index_row, Index_col)) {
					comp.setBackground(tableShadeColors);
				} 
				else {
					comp.setBackground(tableColors);
				}
				return comp;
			}
			//Creates an instance of DefaultTableCellRenderer
			 DefaultTableCellRenderer renderCenter = new DefaultTableCellRenderer();

	         {//initializer block
	             renderCenter.setHorizontalAlignment(SwingConstants.CENTER);
	         }

	          @Override
	       public TableCellRenderer getCellRenderer(int Index_row, int Index_col) {
	              return renderCenter; // returns the object of DefaultTableCellRenderer
	       }	 
		};
		
		table.setModel(model);
		table.getTableHeader().setReorderingAllowed(false);
		table.setRowSorter(sorter = new TableRowSorter(model));
		sorter.setSortsOnUpdates(true);
		sorter.setSortable(0, false);
		sorter.setSortable(1, false);
		sorter.setSortable(2, true);
		table.getRowSorter().toggleSortOrder(2);
		table.getRowSorter().toggleSortOrder(2); // reverses the sort order
		
		// default colors of table
		table.setBackground(tableColors);
		table.setSelectionForeground(Color.BLACK);
		table.setGridColor(tableColors); 
		
		
		// Columns width are setted by index number
		table.getColumnModel().getColumn(0).setPreferredWidth(40);
		table.getColumnModel().getColumn(1).setPreferredWidth(40);
		table.getColumnModel().getColumn(2).setPreferredWidth(100);
		
		// User can't resize the columns
		table.getTableHeader().setResizingAllowed(false);
		
		scrollPane = new JScrollPane(table);
		scrollPane.setBackground(tableColors);
		scrollPane.getViewport().setBackground(tableColors);
		scrollPane.setBorder(BorderFactory.createEmptyBorder());
		scrollPane.setBounds(25, 60, 700, 400);
		scrollPane.setForeground(tableColors);
		scrollPane.getVerticalScrollBar();
		add(scrollPane);
		
		
		
	}
	/**
	 * @return the sorter
	 */
	public TableRowSorter getSorter() {
		return sorter;
	}
	/**
	 * @param sorter the sorter to set
	 */
	public void setSorter(TableRowSorter sorter) {
		this.sorter = sorter;
	}
	/**
	 * @return the model
	 */
	public DefaultTableModel getModel() {
		return model;
	}
	/**
	 * @param model the model to set
	 */
	public void setModel(DefaultTableModel model) {
		this.model = model;
	}
	/**
	 * @return the table
	 */
	public JTable getTable() {
		return table;
	}
	/**
	 * @param table the table to set
	 */
	public void setTable(JTable table) {
		this.table = table;
	}
	/**
	 * @return the scrollPane
	 */
	public JScrollPane getScrollPane() {
		return scrollPane;
	}
	/**
	 * @param scrollPane the scrollPane to set
	 */
	public void setScrollPane(JScrollPane scrollPane) {
		this.scrollPane = scrollPane;
	}
	/**
	 * @param tableColors the tableColors to set
	 */
	public void setTableColors(Color tableColors) {
		this.tableColors = tableColors;
	}

	
}
